using System;
using System.Collections.Generic;
using System.Drawing;

//using Nairo.Editor.Drawing;

namespace Nairo.Parser.Dom
{
	public class FileUnit : INode
	{
		List<INode> children;
		object userData;
		StyleCollection styles;
//		GlobalCSSTable globalStyles;
		
//		public GlobalCSSTable GlobalStyles {
//			get { return globalStyles; }
//			set { globalStyles = value; }
//		}
		
		public bool HasChildNodes {
			get { return this.children.Count > 0;}
		}
		
		public FileUnit() : this(new List<INode>(), null)
		{
		}
		
		public FileUnit(List<INode> children, object userData)
		{
			this.children = children;
			this.userData = userData;
			this.styles = new StyleCollection();
//			this.globalStyles = new GlobalCSSTable(null);
		}
		
		public INode Parent {
			get {
				throw new InvalidOperationException("FileUnit is the topmost item. It has no parent.");
			}
			set {
				throw new InvalidOperationException("FileUnit is the topmost item. It has no parent.");
			}
		}
		
		public FileUnit File {
			get {
				return this;
			}
		}
		
		public StyleCollection Styles {
			get {
				return this.styles;
			}
			set {
				this.styles = value;
			}
		}
		
		public List<INode> Children {
			get { return this.children; }
			set { this.children = value; }
		}
		
		public HtmlBody Body {
			get {
				INode html = GetTag("html");
				if (html == null)
					return null;
				return html.GetTag("body") as HtmlBody;
			}
		}
		
		public HtmlTag Head {
			get {
				INode html = GetTag("html");
				if (html == null)
					return null;
				return html.GetTag("head") as HtmlTag;
			}
		}
		
		public INode[] GetTags(string name)
		{
			List<INode> list = new List<INode>();
			
			foreach (INode node in children) {
				if (node is HtmlTag) {
					HtmlTag tmp = node as HtmlTag;
					if (tmp.TagName == name)
						list.Add(node);
				}
			}
			
			return list.ToArray();
		}
		
		public INode GetTag(string name)
		{
			foreach (INode node in children) {
				if (node is HtmlTag) {
					HtmlTag tmp = node as HtmlTag;
					if (tmp.TagName == name)
						return node;
				}
			}
			
			return null;
		}
		
		public object UserData {
			get {
				return this.userData;
			}
			set {
				this.userData = value;
			}
		}
		
		public object AcceptChildren(IDomVisitor visitor, object data)
		{
			throw new NotImplementedException();
		}
		
		public object AcceptVisitor(IDomVisitor visitor, object data)
		{
			return visitor.VisitFileUnit(this, data);
		}
	}
	

}
